package dungnt.vpoint.mathtest.highscore;

import java.util.ArrayList;
import java.util.List;

import dungnt.vpoint.mathtest.db.User;
import dungnt.vpoint.mathtest.GameMode;
import dungnt.vpoint.mathtest.MainActivity;
import dungnt.vpoint.mathtest.PlayActivity;
import dungnt.vpoint.mathtest.R;
import dungnt.vpoint.mathtest.db.ScoreDatabase;
import dungnt.vpoint.mathtest.db.ScoreAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class HighScoreMain extends Activity implements OnClickListener {

    Button btnAddtion, btnSubtraction, btnMultiplication, btnDivision, btnFinalTest;
    Button btnNoThanks;
    TextView lblHighScoreTitle;
    ScoreDatabase scoreData;
    int bg;
    List<User> array;
    private static ListView lv;
    public static Context context;
    Typeface titleFont, userFont;
    Intent i_home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Dialog highscore_dialog = new Dialog(this);
        highscore_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        highscore_dialog.setContentView(R.layout.highscore_dialog);
        highscore_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        highscore_dialog.show();

        scoreData = new ScoreDatabase(this);
        scoreData.openDB();
        array = new ArrayList<User>();
        context = this;

        titleFont = Typeface.createFromAsset(HighScoreMain.context.getAssets(), "MYRIADPRO-REGULAR.OTF");
        userFont = Typeface.createFromAsset(HighScoreMain.context.getAssets(), "arial_regular.ttf");

        btnAddtion = (Button) highscore_dialog.findViewById(R.id.btnAddtion);
        btnSubtraction = (Button) highscore_dialog.findViewById(R.id.btnSubtraction);
        btnMultiplication = (Button) highscore_dialog.findViewById(R.id.btnMultiplication);
        btnDivision = (Button) highscore_dialog.findViewById(R.id.btnDivision);
        btnFinalTest = (Button) highscore_dialog.findViewById(R.id.btnFinal);
        btnNoThanks = (Button) highscore_dialog.findViewById(R.id.alert3_nothanks);
        lblHighScoreTitle = (TextView) highscore_dialog.findViewById(R.id.lblHighScoreTitle);
        lblHighScoreTitle.setText("Math Test: Add Test Score");
        lblHighScoreTitle.setTypeface(titleFont);

        btnAddtion.setOnClickListener(this);
        btnSubtraction.setOnClickListener(this);
        btnMultiplication.setOnClickListener(this);
        btnDivision.setOnClickListener(this);
        btnFinalTest.setOnClickListener(this);
        btnNoThanks.setOnClickListener(this);

        lv = (ListView) highscore_dialog.findViewById(R.id.lv_highscore);

        Cursor cursor = scoreData.getHighScoresAdd();
        if (cursor.moveToFirst()) {
            do {
                array.add(new User(cursor.getString(2), cursor.getInt(3), cursor.getString(4)));
            } while (cursor.moveToNext());
        }
        ScoreAdapter adapter = new ScoreAdapter(getBaseContext(), array);
        lv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnAddtion:
            bg = 0;
            setBackgroundButton(bg);
            lblHighScoreTitle.setText("Math Test: Add Test Score");

            lv.invalidateViews();
            array.clear();

            Cursor cursor = scoreData.getHighScoresAdd();
            if (cursor.moveToFirst()) {
                do {
                    array.add(new User(cursor.getString(2), cursor.getInt(3), cursor.getString(4)));

                } while (cursor.moveToNext());
            }
            ScoreAdapter adapter = new ScoreAdapter(getBaseContext(), array);
            lv.setAdapter(adapter);

            break;
        case R.id.btnSubtraction:
            bg = 1;
            setBackgroundButton(bg);
            lblHighScoreTitle.setText("Math Test: Subtract Test Score");

            lv.invalidateViews();
            array.clear();

            Cursor cursor1 = scoreData.getHighScoresSubtract();
            if (cursor1.moveToFirst()) {
                do {
                    array.add(new User(cursor1.getString(2), cursor1.getInt(3), cursor1.getString(4)));

                } while (cursor1.moveToNext());
            }
            ScoreAdapter adapter1 = new ScoreAdapter(getBaseContext(), array);
            lv.setAdapter(adapter1);
            break;
        case R.id.btnMultiplication:
            bg = 2;
            setBackgroundButton(bg);
            lblHighScoreTitle.setText("Math Test: Multiply Test Score");

            lv.invalidateViews();
            array.clear();

            Cursor cursor2 = scoreData.getHighScoresMultiply();
            if (cursor2.moveToFirst()) {
                do {
                    array.add(new User(cursor2.getString(2), cursor2.getInt(3), cursor2.getString(4)));

                } while (cursor2.moveToNext());
            }
            ScoreAdapter adapter2 = new ScoreAdapter(getBaseContext(), array);
            lv.setAdapter(adapter2);
            break;
        case R.id.btnDivision:
            bg = 3;
            setBackgroundButton(bg);
            lblHighScoreTitle.setText("Math Test: Divide Test Score");

            lv.invalidateViews();
            array.clear();

            Cursor cursor3 = scoreData.getHighScoresDevide();
            if (cursor3.moveToFirst()) {
                do {
                    array.add(new User(cursor3.getString(2), cursor3.getInt(3), cursor3.getString(4)));

                } while (cursor3.moveToNext());
            }
            ScoreAdapter adapter3 = new ScoreAdapter(getBaseContext(), array);
            lv.setAdapter(adapter3);
            break;
        case R.id.btnFinal:
            bg = 4;
            setBackgroundButton(bg);
            lblHighScoreTitle.setText("Math Test: Final Test Score");

            lv.invalidateViews();
            array.clear();

            Cursor cursor4 = scoreData.getHighScoresFinal();
            if (cursor4.moveToFirst()) {
                do {
                    array.add(new User(cursor4.getString(2), cursor4.getInt(3), cursor4.getString(4)));

                } while (cursor4.moveToNext());
            }
            ScoreAdapter adapter4 = new ScoreAdapter(getBaseContext(), array);
            lv.setAdapter(adapter4);
            break;
        case R.id.alert3_nothanks:
            scoreData.closeDB();
            i_home = new Intent(HighScoreMain.this, MainActivity.class);
            startActivity(i_home);
            finish();
            break;
        }
    }

    public void setBackgroundButton(int a)

    {
        switch (a) {
        case 0:
            btnAddtion.setBackgroundResource(R.drawable.btn_addtion_pressed);
            btnSubtraction.setBackgroundResource(R.drawable.btn_subtraction_normal);
            btnMultiplication.setBackgroundResource(R.drawable.btn_multiplication_normal);
            btnDivision.setBackgroundResource(R.drawable.btn_division_normal);
            btnFinalTest.setBackgroundResource(R.drawable.btn_final_normal);
            break;
        case 1:
            btnAddtion.setBackgroundResource(R.drawable.btn_addtion_normal);
            btnSubtraction.setBackgroundResource(R.drawable.btn_subtraction_pressed);
            btnMultiplication.setBackgroundResource(R.drawable.btn_multiplication_normal);
            btnDivision.setBackgroundResource(R.drawable.btn_division_normal);
            btnFinalTest.setBackgroundResource(R.drawable.btn_final_normal);
            break;
        case 2:
            btnAddtion.setBackgroundResource(R.drawable.btn_addtion_normal);
            btnSubtraction.setBackgroundResource(R.drawable.btn_subtraction_normal);
            btnMultiplication.setBackgroundResource(R.drawable.btn_multiplication_pressed);
            btnDivision.setBackgroundResource(R.drawable.btn_division_normal);
            btnFinalTest.setBackgroundResource(R.drawable.btn_final_normal);
            break;
        case 3:
            btnAddtion.setBackgroundResource(R.drawable.btn_addtion_normal);
            btnSubtraction.setBackgroundResource(R.drawable.btn_subtraction_normal);
            btnMultiplication.setBackgroundResource(R.drawable.btn_multiplication_normal);
            btnDivision.setBackgroundResource(R.drawable.btn_division_pressed);
            btnFinalTest.setBackgroundResource(R.drawable.btn_final_normal);
            break;
        case 4:
            btnAddtion.setBackgroundResource(R.drawable.btn_addtion_normal);
            btnSubtraction.setBackgroundResource(R.drawable.btn_subtraction_normal);
            btnMultiplication.setBackgroundResource(R.drawable.btn_multiplication_normal);
            btnDivision.setBackgroundResource(R.drawable.btn_division_normal);
            btnFinalTest.setBackgroundResource(R.drawable.btn_final_press);
            break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
            finish();
        }

        return super.onKeyDown(keyCode, event);
    }
}
